package com.budwk.app.miniapp.services.impl;

import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.app.miniapp.models.WxMsg;
import com.budwk.app.miniapp.services.WxmsgService;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;

@IocBean(args = {"refer:dao"})
public class WxmsgServiceImpl extends BaseServiceImpl<WxMsg> implements WxmsgService {
    public WxmsgServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination listWxMsgMapGroup(int pageNo, int pageSize, WxMsg bean) {
        StringBuffer sb = new StringBuffer();
        sb.append("select a.*, b.count_msg from wx_msg as a right join (select `wx_user_id`,max(`create_time`) as maxtime count(`wx_user_id`) as count_msg from wx_msg");
        sb.append(" WHERE 1 = 1");
        if (StringUtils.isNotEmpty(bean.getType())) {
            sb.append(" AND `type` = @type");
        }
        if (StringUtils.isNotEmpty(bean.getReadFlag())) {
            sb.append(" AND `read_flag` = @readFlag");
        }
        if (StringUtils.isNotEmpty(bean.getNotInRepType())) {
            sb.append(" AND `rep_type` NOT IN (@notInRepType)");
        }
        sb.append("group by `wx_user_id`) as b on a.`wx_user_id` = b.`wx_user_id` and a.`create_time` = b.maxtime order by a.`create_time` desc");
        Sql sql = Sqls.create(sb.toString())
                .setParam("type", bean.getType())
                .setParam("readFlag", bean.getReadFlag())
                .setParam("notInRepType", bean.getNotInRepType());

        return this.listPage(pageNo, pageSize, sql);
    }
}
